package fjut.acm.coding.controller;

import fjut.acm.coding.config.interceptor.LoginRequired.LoginRequired;
import fjut.acm.coding.entity.MallGoods;
import fjut.acm.coding.entity.result.ResultCode;
import fjut.acm.coding.entity.result.ResultJson;
import fjut.acm.coding.service.MallGoodsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@RestController
@CrossOrigin
@RequestMapping("/mall")
public class MallController {
    @Resource
    MallGoodsService mallGoodsService;

    @GetMapping("/list")
    public ResultJson getMallGoodsList(@RequestParam("pageNum") Integer pageNum,
                                       @RequestParam("pageSize") Integer pageSize) {
        ResultJson resultJson = new ResultJson(ResultCode.REQUIRED_SUCCESS);
        List<MallGoods> mallGoods = mallGoodsService.pages(pageNum, pageSize);
        Integer count = mallGoodsService.selectAllCount();
        resultJson.addInfo(mallGoods);
        resultJson.addInfo(count);
        return resultJson;
    }

    @LoginRequired
    @GetMapping("/info")
    public ResultJson getMallGoods(@RequestParam("id") Integer id) {
        ResultJson resultJson = new ResultJson(ResultCode.REQUIRED_SUCCESS);
        MallGoods mallGoods = mallGoodsService.selectByGoodsId(id);
        if (Objects.isNull(mallGoods)) {
            resultJson.setStatus(ResultCode.RESOURCE_NOT_EXIST);
        } else {
            resultJson.addInfo(mallGoods);
        }
        return resultJson;
    }

}

